package xcmg.device.service.warehouse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.warehouse.WarehouseInventoryDO;
import xcmg.device.dao.entity.warehouse.WarehouseInventorySplitDO;
import xcmg.device.dao.mapper.warehouse.WarehouseInventoryMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseInventorySplitMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.warehouse.SplitDetailVO;
import xcmg.device.service.vo.warehouse.SplitVO;
import xcmg.device.service.vo.warehouse.WarehouseInventoryVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.ListUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class WarehouseInventorySplitService {

    @Autowired
    private WarehouseInventorySplitMapper warehouseInventorySplitMapper;

    @Autowired
    private WarehouseInventoryMapper warehouseInventoryMapper;

    @Autowired
    private FastGenClient fastGenClient;

    /**
     * @Author: WangKe
     * @Description: 新增在库表条码拆分记录
     * @Date: 2018/2/6 0006
     */
    @Transactional(rollbackFor = Exception.class)
    public ErrorCode add(SplitVO splitVO)throws Exception{

        List<SplitDetailVO> detailList = splitVO.getDetailList();
        if(ListUtil.isNullOrEmpty(detailList)){
            return ErrorCode.IllegalArument;
        }
        WarehouseInventoryVO inventoryVO = new WarehouseInventoryVO();
        inventoryVO.setBarCode(splitVO.getPreBarCode());
        inventoryVO.setOrgId(splitVO.getOrgId());
        List<WarehouseInventoryDO> inventoryDOList = warehouseInventoryMapper.list(inventoryVO);
        if(ListUtil.isNullOrEmpty(inventoryDOList)){
            throw new Exception("在库表中找不到相关条码");
        }
        WarehouseInventoryDO inventoryDO = inventoryDOList.get(0);

        WarehouseInventorySplitDO splitDO;
        for (SplitDetailVO vo : detailList){
            ActionResult<String> textResult = fastGenClient.textGuid();
            if (textResult.getCode() != ErrorCode.Success.getCode())
            {
                return ErrorCode.IllegalArument;
            }
            splitDO = new WarehouseInventorySplitDO();
            splitDO.setId(textResult.getValue());
            splitDO.setPreBarCode(splitVO.getPreBarCode());
            splitDO.setNowBarCode(vo.getNowBarCode());
            splitDO.setNum(vo.getNum());
            splitDO.setCreaterId(splitVO.getUserId());
            splitDO.setOrgId(splitVO.getOrgId());
            splitDO.setCreateDate(new Date());
            warehouseInventorySplitMapper.insert(splitDO);
            WarehouseInventoryDO warehouseInventoryDO=new WarehouseInventoryDO();
            BeanUtils.copyProperties(inventoryDO,warehouseInventoryDO);
            warehouseInventoryDO.setId(textResult.getValue());
            warehouseInventoryDO.setBarCode(vo.getNowBarCode());
            warehouseInventoryDO.setAmount(vo.getNum());
            warehouseInventoryDO.setCreaterId(splitVO.getUserId());
            warehouseInventoryDO.setCreateDate(new Date());
            warehouseInventoryMapper.insert(warehouseInventoryDO);
        }
        //更新原条码状态
        inventoryDO.setStatus(4);
        inventoryDO.setIsOut(1);
        warehouseInventoryMapper.updateByPrimaryKeySelective(inventoryDO);
        return ErrorCode.Success;
    }

    public ErrorCode splitNumber(String orgId,String barcode,String newBarcode,BigDecimal num,String userId)throws Exception{
        WarehouseInventoryVO inventoryVO = new WarehouseInventoryVO();
        inventoryVO.setBarCode(barcode);
        inventoryVO.setOrgId(orgId);
        List<WarehouseInventoryDO> inventoryDOList = warehouseInventoryMapper.list(inventoryVO);
        if(ListUtil.isNullOrEmpty(inventoryDOList)){
            throw new Exception("在库表中找不到相关条码");
        }
        WarehouseInventoryDO inventoryDO = inventoryDOList.get(0);

        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode())
        {
            return ErrorCode.IllegalArument;
        }
        WarehouseInventorySplitDO splitDO = new WarehouseInventorySplitDO();
        splitDO.setId(textResult.getValue());
        splitDO.setPreBarCode(barcode);
        splitDO.setNowBarCode(newBarcode);
        splitDO.setNum(num);
        splitDO.setCreaterId(userId);
        splitDO.setOrgId(orgId);
        splitDO.setCreateDate(new Date());
        warehouseInventorySplitMapper.insert(splitDO);
        WarehouseInventoryDO warehouseInventoryDO=new WarehouseInventoryDO();
        BeanUtils.copyProperties(inventoryDO,warehouseInventoryDO);
        warehouseInventoryDO.setId(textResult.getValue());
        warehouseInventoryDO.setBarCode(newBarcode);
        warehouseInventoryDO.setAmount(num);
        warehouseInventoryDO.setCreaterId(userId);
        warehouseInventoryDO.setLastChangeDate(new Date());
        warehouseInventoryMapper.insert(warehouseInventoryDO);
        inventoryDO.setAmount((inventoryDO.getAmount().subtract(num)));
        warehouseInventoryMapper.updateByPrimaryKeySelective(inventoryDO);
        return ErrorCode.Success;
    }
}
